740B - Alyona and flowers - CodeForces Solution


constructive algorithms *1200

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
arr = list(map(int, input().split()))
score = 0

ps = [arr[0]]
for i in range(1, n):
	s = ps[i-1] + arr[i]
	ps.append(s)

for i in range(m):
	l, r = map(int, input().split())
	
	if (l == 1):
		total = ps[r-1]
	else:
		total = ps[r-1] - ps[l-2]
		
	score = max(score, total+score)

print(score)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector <int> v(n+1);
    for(int i = 1; i<=n; i++)
    {
        cin >> v[i];
    }
    vector<int> cnt(n+1);
    for(int i = 1; i<=m; i++)
    {
        int a, b;
        cin >> a >> b;
        int sum = 0;
        for(int j = a; j<=b; j++)
        {
            sum = sum + v[j];
        }
        if(sum>0)
        {
            for(int x = a; x<=b; x++)
                cnt[x]++;
        }

    }
    int ans = 0;
    for(int i = 1; i<=n; i++)
    {
        ans = ans + v[i]*cnt[i];
    }
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz